#include "home.h"
#include <QLabel>
#include <QString>
#include <QPixmap>
#include <QApplication>

home::home(QWidget *parent)
    : QWidget(parent)
{
    homeParaInit(); // 初始化窗口
}

home::~home()
{

}

// 窗口进行设置并初始化
void home::homeParaInit()
{
    // 设置主窗口文本标题
    this->setWindowTitle("模拟路由器");
    // 设置窗口固定显示大小
    this->setFixedSize(640, 480);

    // 设置窗口图标
    QIcon icon(":/icon/title.png");
    this->setWindowIcon(icon);

    // 页面初始化
    this->QStackWidget = new QStackedWidget(this);
    this->QStackWidget->setFixedSize(600, 350);
    this->arpPage0 = new QWidget(this);
    this->blackPage1 = new QWidget(this);
    this->QStackWidget->addWidget(arpPage0);
    this->QStackWidget->addWidget(blackPage1);

    // 按键初始化
    QpBut0 = new QPushButton(this); // 开关机按键
    QpBut0->setFixedSize(80, 80);
    QpBut0->move(20, 380);
    QpBut1 = new QPushButton(this); // ARP扫描按键
    QpBut1->setFixedSize(80, 80);
    QpBut1->move(280, 380);
    QpBut2 = new QPushButton(this); // 黑名单按键
    QpBut2->setFixedSize(80, 80);
    QpBut2->move(this->width()-100, 380);

    // 设置按钮为扁平选中类型
    QpBut0->setFlat(true);
    QpBut1->setFlat(true);
    QpBut2->setFlat(true);

    // 按键美化
    QIcon icon0;
    QIcon icon1;
    QIcon icon2;

    icon0.addFile(tr(":/icon/off.png"));
    icon1.addFile(tr(":/icon/scan.png"));
    icon2.addFile(tr(":/icon/blacklist.png"));

    // 按键添加图标
    QpBut0->setIcon(icon0);
    QpBut0->setIconSize(QSize(80, 80));
    QpBut1->setIcon(icon1);
    QpBut1->setIconSize(QSize(80, 80));
    QpBut2->setIcon(icon2);
    QpBut2->setIconSize(QSize(80, 80));

    // 路由器状态显示标题
    onOffTitle = new QLabel(this);
    onOffTitle->setText("ShutDown");
    onOffTitle->move(10, 10);

    connect(QpBut0, &QPushButton::clicked, this, &home::qPBut0Clicked);
    connect(QpBut1, &QPushButton::clicked, this, &home::qPBut1Clicked);
    connect(QpBut2, &QPushButton::clicked, this, &home::qPBut2Clicked);

    SlidingWindowInit();
}

// 滑动窗口初始化
void home::SlidingWindowInit()
{
    // 设置歌词目录滑动窗口
    QWidgetSArea0 = new QScrollArea;
    QWidgetSArea0->setStyleSheet("background-color: white;");
    QWidgetSArea0->setFixedSize(600, 300);

    QSBar0 = new QScrollArea(this->arpPage0);
    QSBar0->setFixedSize(580,270);
    QSBar0->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QSBar0->setWidget(QWidgetSArea0);
    QSBar0->move(20, 30);

    // 设置歌词窗口滑动窗口
    QWidgetSArea1 = new QScrollArea;
    QWidgetSArea1->setStyleSheet("background-color: white;");
    QWidgetSArea1->setFixedSize(600, 300);

    QSBar1 = new QScrollArea(this->blackPage1);
    QSBar1->setFixedSize(580,270);
    QSBar1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QSBar1->setWidget(QWidgetSArea1);
    QSBar1->move(20, 30);

    this->QStackWidget->setVisible(false); // 隐藏 QStackedWidget
}

// 路由器开关控制
void home::qPBut0Clicked(bool checked)
{
    QIcon icon;

    if(!checked){
        this->onOffFlag = !this->onOffFlag;
    }
    if(!this->onOffFlag){
        onOffTitle->clear();
        onOffTitle->setText("ShutDown");
        icon.addFile(tr(":/icon/off.png"));
    }else{
        onOffTitle->clear();
        onOffTitle->setText("PowerOn");
        icon.addFile(tr(":/icon/on.png"));   
    }

    // 按键添加图标
    QpBut0->setIcon(icon);
    QpBut0->setIconSize(QSize(80, 80));
}

// ARP扫描按键控制函数
void home::qPBut1Clicked(bool checked)
{
    this->QStackWidget->setVisible(true);
    this->QStackWidget->setCurrentIndex(0);
    if(this->onOffFlag){
        if(!checked){
            onOffTitle->clear();
            onOffTitle->setFixedWidth(100);
            onOffTitle->setText("ARP扫描名单:");
        }
    }
}

// 黑名单显示控制按键
void home::qPBut2Clicked(bool checked)
{
    this->QStackWidget->setVisible(true);
    this->QStackWidget->setCurrentIndex(1);
    if(this->onOffFlag){
        if(!checked){
            onOffTitle->clear();
            onOffTitle->setText("黑名单:");
        }
    }
}




















